#include "editdictdatadlg.h"
#include "ui_editdictdatadlg.h"

EditDictDataDlg::EditDictDataDlg(int id,QString type,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditDictDataDlg)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    setWindowTitle("编辑字典数据");

    this->type = type;
    this->id = id;
    if(id > 0){
        ui->label->setText("编辑字典数据");
    }

    init();
}

EditDictDataDlg::~EditDictDataDlg()
{
    delete ui;
}

void EditDictDataDlg::init()
{
    ui->lineEdit_dictType->setText(type);
    QStringList keys = utils.backShowStyleKeys;
    for(int i = 0;i< keys.size();i++){
        QString key = keys.at(i);
        ui->comboBox->addItem(key,key.mid(3, key.indexOf(")") - 3 ));
    }
    if(id > 0){     //回显
        QByteArray *data = api.getDictDataById(id);
        QJsonObject obj = QJsonDocument::fromJson(*data).object();
        if(obj.value("code").toInt() != 200) return;
        dictData = DictData::toDictData(obj.value("data").toObject());
        ui->lineEdit_dictType->setText(dictData.dictType);
        ui->lineEdit_dataLabel->setText(dictData.dictLabel);
        ui->lineEdit_dataValue->setText(dictData.dictValue);
        ui->lineEdit_styleAttr->setText(dictData.cssClass);
        ui->spinBox->setValue(dictData.dictSort);
        for(int i = 0;i< ui->comboBox->count();i++){
            if(ui->comboBox->itemData(i).value<QString>().contains(dictData.listClass)){

                ui->comboBox->setCurrentIndex(i);
                break;
            }
        }
        dictData.status == "0" ? ui->radioButton_normal->setChecked(1) : ui->radioButton_stop->setChecked(1);
        ui->textEdit->setText(dictData.remark);
    }
}

void EditDictDataDlg::on_btn_ok_clicked()
{
    dictData.dictType = ui->lineEdit_dictType->text();
    dictData.dictLabel = ui->lineEdit_dataLabel->text();
    dictData.dictValue = ui->lineEdit_dataValue->text();
    dictData.cssClass = ui->lineEdit_styleAttr->text();
    dictData.dictSort = ui->spinBox->value();
    dictData.listClass = ui->comboBox->itemData(ui->comboBox->currentIndex()).value<QString>();
    dictData.status = ui->radioButton_normal->isChecked() ? "0" : "1";
    dictData.remark = ui->textEdit->toPlainText();
    QByteArray *data = api.editDictData(this->id,dictData);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() != 200) return;
    QMessageBox::information(this,"提示",obj.value("msg").toString());
    emit success();
    close();
}


void EditDictDataDlg::on_btn_cancel_clicked()
{
    close();
}

